import React, { Component } from 'react'
import { Row, Col, Input, Button,message } from 'antd';
import axios from '../../http/http';
import './index.css'
export default class index extends Component {
    state = {
        username: "",
        password: ""
    }

    saveFormData = (dataType) => {
        return (e) => {
            this.setState({ [dataType]: e.target.value })
        }
    }

    login = async () => {
        const {username,password}=this.state
        const {data}=await axios({
            method:"POST",
            url:'/login',
            data:{
                username:username,
                password:password
            }
        })
        if(data.code===0){
            sessionStorage.setItem("token", data.data);
            message.success('登录成功',1);
            this.props.history.push("/main")
        }
    }
    render() {
        return (
            <div className='container'>
                <Row justify="center" align="middle" style={{ height: "100vh" }}>
                    <Col span={8}>
                        <div className="login-air">
                            <Row justify="center">
                                <Col span={10}>
                                    <div className="title">智慧养老后台管理系统</div>
                                </Col>
                            </Row>
                            <Row justify="center" className="uname">
                                <Col span={2}>账号：</Col>
                                <Col span={14}>
                                    <Input size="large" placeholder="请输入账号" onChange={this.saveFormData("username")} />
                                </Col>
                            </Row>
                            <Row justify="center" className="password">
                                <Col span={2}>密码：</Col>
                                <Col span={14}>
                                    <Input.Password size="large" placeholder="请输入密码" onChange={this.saveFormData("password")} />
                                </Col>
                            </Row>
                            <Row justify="center">
                                <Col span={14} offset={2}>
                                    <Button type="primary" className="login-btn" onClick={this.login}>登录</Button>
                                </Col>
                            </Row>
                        </div>
                    </Col>
                </Row>
            </div>
        )
    }
}
